<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>微信登录回调页面</title>

    <script src="plugins/vue/dist/vue.js"></script>
    <script src="plugins/axios/dist/axios.js"></script>
    <script src="script/common.js"></script>
</head>
<body>
    <div id="app"></div>
    <!--回调页面用户扫码成功以后,要跳转的页面
    获取微信返回的code,拿到这个code调用后端的接口-->

    <script type="text/javascript">
        new Vue({
            el:"#app",
            data:{
                code:""
            },
            methods:{
                wxLogin(){
                    this.$http.post("/login/wechat/"+this.code)
                        .then(result => {
                            result = result.data;
                            if(result.success){
                                let data = result.objData;
                                if(data.message == "noBind"){
                                    // 没有绑定,要跳转到绑定页面
                                    let {accessToken,openid} = data;
                                    location.href = "/binder.html?accessToken="+accessToken+"&openid="+openid;
                                }else{
                                    // 说明登录成功,将token和loginUser存入localStorage,跳转到首页
                                    alert("登录成功");
                                    // 将后端返回的token和loginUser存入localStorage
                                    localStorage.setItem("token",result.objData.token);
                                    // 存信息到localStorage的时候,不能直接存对象,而要转成json的字符串才可以
                                    localStorage.setItem("loginUser",JSON.stringify(result.objData.loginUser));
                                    // 跳转到首页
                                    location.href = "/index.html";
                                }
                            }else{
                                alert(result.msg);
                            }
                        })
                }
            },
            mounted(){
                // http://wx.rongcarservice.com/callback.html?code=011AXv0w3EkXY0355A2w3gDuLm0AXv05&state=
                // 获取url中code,调用后端接口
                let url = location.href;
                let obj = url2Param(url);   // {code:""}
                this.code = obj.code;
                this.wxLogin();
            }
        })
    </script>
</body>
</html>